import React, { CSSProperties } from 'react';
import { ColumnProps } from 'antd/lib/table';
import _ from 'lodash';
import { StateType } from '@/common-typings';
import { ORDER_TYPE_DETAIL, REPORT_TRANSFER_STATUS, USER_TYPE } from '@/pages/finance/report/data';
import * as tools from '@/utils/tools';
import { toTrans } from '@/pages/finance/report/until';

export function getTransationColumns(tableFilters?: any, getColumnSearchProps?: any) {
  const greenTextStyle: CSSProperties = {
    color: '#97C586',
  };
  const orangeTextStyle: CSSProperties = {
    color: '#FFA427',
  };
  const redTextStyle: CSSProperties = {
    color: '#FF1314',
  };
  let userTypeData;
  let statusData;

  if (tableFilters) {
    const { userType, status } = tableFilters;
    userTypeData = userType ? [userType] : ['-1'];
    statusData = status ? [status] : ['-1'];
  }

  const columns: ColumnProps<StateType>[] = [
    {
      title: '交易订单号',
      dataIndex: 'orderId',
      width: 230,
    },
    {
      title: '账户名',
      dataIndex: 'username',
      width: 90,
    },
    {
      title: '账户类型',
      dataIndex: 'userType',
      width: 120,
      render: text => toTrans(USER_TYPE, text),
      filters: [
        { text: '全部', value: '-1' },
        { text: '会员', value: '0' },
        { text: '代理', value: '1' },
      ],
      filteredValue: userTypeData,
      filterMultiple: false,
    },
    {
      title: '所属代理',
      dataIndex: 'parentName',
      width: 90,
      ...getColumnSearchProps('parentName'),
    },
    {
      title: '交易类型',
      dataIndex: 'orderDesc',
      width: 90,
      // render: text => toTrans(ORDER_TYPE_DETAIL, text.toString()),
    },
    {
      title: '细分类型',
      dataIndex: 'subType',
      width: 90,
      render: text => text || '-',
    },
    {
      title: '操作动作',
      dataIndex: 'remark',
      width: 100,
    },
    {
      title: '操作时间',
      dataIndex: 'createTime',
      render: text => tools.toTime(text),
      width: 100,
      sorter: (a, b) => a.createTime - b.createTime,
    },
    {
      title: '交易金额',
      dataIndex: 'amount',
      width: 130,
      render: text => tools.currency(text),
    },
    {
      title: '手续费',
      dataIndex: 'fee',
      width: 130,
      render: text => tools.currency(text),
    },
    {
      title: '应到账',
      dataIndex: 'reallyAmount',
      width: 130,
      render: text => tools.currency(text),
    },
    {
      title: '中心钱包帐变前余额',
      dataIndex: 'balanceBefore',
      width: 140,
      render: (text, record) => {
        const val = record.status === 0 && record.orderType !== 2 ? '-' : tools.currency(text);
        return val;
      },
    },
    {
      title: '状态',
      dataIndex: 'status',
      width: 90,
      render: text => {
        let style;
        if (text === 1) {
          style = greenTextStyle;
        } else if (text === 0) {
          style = orangeTextStyle;
        } else if (text === 2) {
          style = redTextStyle;
        }
        return <span style={style}>{toTrans(REPORT_TRANSFER_STATUS, text)}</span>;
      },
      filters: [
        { text: '全部', value: '-1' },
        { text: '处理中', value: '0' },
        { text: '成功', value: '1' },
        { text: '失败', value: '2' },
      ],
      filteredValue: statusData,
      filterMultiple: false,
    },
    {
      title: '中心钱包帐变后余额',
      dataIndex: 'balance',
      width: 140,
      render: (text, record) => {
        const val = record.status === 0 && record.orderType !== 2 ? '-' : tools.currency(text);
        return val;
      },
    },
    {
      title: '帐变时间',
      dataIndex: 'finishTime',
      width: 100,
      render: text => tools.toTime(text),
    },
  ];
  return columns;
}

export const formatObj = {
  userType: (val: any) => toTrans(USER_TYPE, val),
  orderType: (val: any) => toTrans(ORDER_TYPE_DETAIL, val.toString()),
  createTime: (val: any) => tools.toTime(val),
  amount: (val: any) => tools.currency(val),
  fee: (val: any) => tools.currency(val),
  reallyAmount: (val: any) => tools.currency(val),
  balanceBefore: (val: any, record: any) => {
    const value = record.status === 0 && record.orderType !== 2 ? '' : tools.currency(val);
    return value;
  },
  status: (val: any) => toTrans(REPORT_TRANSFER_STATUS, val),
  balance: (val: any, record: any) => {
    const value = record.status === 0 && record.orderType !== 2 ? '' : tools.currency(val);
    return value;
  },
  finishTime: (val: any) => tools.toTime(val),
};

export function checkHasChanged(tableFilters: object, filters: object): boolean {
  let flag = false;
  const filtersKeys = Object.keys(filters);
  _.forEach(filtersKeys, item => {
    const filtersItemValue = filters[item].join(',');
    if (filtersItemValue !== tableFilters[item]) {
      flag = true;
    }
  });
  return flag;
}
